<?php

include_once('ccl_controller.php');

class unit_testing extends ccl_controller
{
	function __construct()
	{
		// Call the Model constructor
		parent::__construct();
		
		// Load database with "admin" permissions
		$this->load->database('admin');
		
		$this->load->library('unit_test');
	}

	public function index()
	{
		// Perform tests
		$this->volumeModelTest();
		$this->subscriberModelTest();
		$this->loanModelTest();
		$this->reportsModelTest();
		
		// Output report
		echo $this->unit->report();
	}
	
	public function volumeModelTest()
	{
	    	// Run Volume Model Tests
		$this->load->model('volume_model');
		
		// Add Volume
		$volume = array('call_number'=>'HW920/HAR/WIL',
				'title'=>'Test Title',
				'location_id'=>'C',
				'catalogue_date'=>date('Y-m-d'));
		$this->unit->run( $this->volume_model->addVolume($volume), 'is_null', 'Add Volume');

		// Get Volume
		$volume = $this->volume_model->getVolumeByID( $this->volume_model->getLastAccessID( ) );
		$this->unit->run($volume, 'is_object', 'View Volume');
		
		// Update Volume
		$update_array = array('title'=>'Updated Title');
		$this->volume_model->updateVolume( $this->volume_model->getLastAccessID( ), $update_array );
		$volume = $this->volume_model->getVolumeByID( $this->volume_model->getLastAccessID( ) );
		$this->unit->run( $this->volume_model->updateVolume( 1, $update_array ), 'is_null', 'Update Volume');
		
		// Delete Volume
		$this->unit->run( $this->volume_model->deleteVolumeByID( 1 ), 'is_null', 'Delete Volume');
	}
	
	public function subscriberModelTest()
	{
	    	// Run Volume Model Tests
		$this->load->model('subscriber_model');
		
		// Add Volume
		$subscriber = array('first_name'=>'Unit',
				'last_name'=>'Testing',
				'address1'=>'123 Test Street',
				'suburb'=>'Testburbia',
				'post_code'=>'3156',
			    	'subscriber_status'=>'A');
		
		$this->unit->run( $this->subscriber_model->addSubscriber($subscriber), 'is_null', 'Add Subscriber');

		// Get Subscriber
		$subscriber = $this->subscriber_model->getSubscriberByID( $this->subscriber_model->getLastSubscriberID() );
		$this->unit->run( $subscriber, 'is_object', 'View Subscriber');
		
		// Update Subscriber
		$update_array = array('last_name'=>'Testing Successful');
		$this->unit->run( $this->subscriber_model->updateSubscriber( $this->subscriber_model->getLastSubscriberID(), $update_array ), 'is_null', 'Update Subscriber');
		
		// Delete Subscriber
		$this->unit->run( $this->subscriber_model->deleteSubscriberByID( $this->subscriber_model->getLastSubscriberID() ), 'is_null', 'Delete Subscriber');	    
	}
	
	public function loanModelTest()
	{
	    	// Run Volume Model Tests
		$this->load->model('subscriber_model');
		
		// Add Volume
		$subscriber = array('first_name'=>'Unit',
				'last_name'=>'Testing',
				'address1'=>'123 Test Street',
				'suburb'=>'Testburbia',
				'post_code'=>'3156',
			    	'subscriber_status'=>'A');
		
		$this->unit->run( $this->subscriber_model->addSubscriber($subscriber), 'is_null', 'Add Loan');

		// Get Subscriber
		$subscriber = $this->subscriber_model->getSubscriberByID( $this->subscriber_model->getLastSubscriberID() );
		$this->unit->run( $subscriber, 'is_object', 'View Loan');
		
		// Update Subscriber
		$update_array = array('last_name'=>'Testing Successful');
		$this->unit->run( $this->subscriber_model->updateSubscriber( $this->subscriber_model->getLastSubscriberID(), $update_array ), 'is_null', 'Update Loan');
		
		// Delete Subscriber
		$this->unit->run( $this->subscriber_model->deleteSubscriberByID( $this->subscriber_model->getLastSubscriberID() ), 'is_null', 'Delete Loan');	    
	}
	
	public function reportsModelTest()
	{
		// Load Reports Model
		$this->load->model('reports_model');
	    
		// Get Results
	        $results = $this->reports_model->getCallNumberRange( 'C', 'HM', 920, 921 );
		$this->unit->run( $results, 'is_object', 'getCallNumberRange()');
		
		// Get Results
	        $results = $this->reports_model->getVolumesByBranch('M');
		$this->unit->run( $results, 'is_object', 'getVolumesByBranch()');
		
		// Get Results
	        $results = $this->reports_model->getOverdueLoans( 'C', date('Y-m-d'));
		$this->unit->run( $results, 'is_object', 'getOverdueLoans()');
		
		// Get Results
	        $results = $this->reports_model->getSubscribersInArrears( date('Y-m-d'));
		$this->unit->run( $results, 'is_object', 'getSubscribersInArrears()');
		
		// Get Results
	        $results = $this->reports_model->getSubscribersInArrearsWithOverdueLoans( date('Y-m-d'));
		$this->unit->run( $results, 'is_object', 'getSubscribersInArrearsWithOverdueLoans()');

		// Get Results
	        $results = $this->reports_model->getFinancialSubscribers( date('Y-m-d'));
		$this->unit->run( $results, 'is_object', 'getFinancialSubscribers()');
	}
}

?>